package com.dfy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dfy.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 保留一些自定义查询方法
    User findById(@Param("userId") Long userId);
    User findByUsername(String username);
    User findByEmail(String email);
    User findByPhone(String phone);

    @Select("SELECT user_id, username, password, email, phone, role, status, created_at, updated_at, is_deleted " +
           "FROM users WHERE phone = #{phone}")
    User findByPhoneForLogin(String phone);

    @Select("SELECT user_id, username, email, phone, role, status, created_at, updated_at, is_deleted " +
           "FROM users " +
           "WHERE (#{username} IS NULL OR username LIKE CONCAT('%', #{username}, '%')) " +
           "AND (#{email} IS NULL OR email LIKE CONCAT('%', #{email}, '%')) " +
           "AND (#{phone} IS NULL OR phone LIKE CONCAT('%', #{phone}, '%')) " +
           "ORDER BY created_at DESC " +
           "LIMIT #{offset}, #{size}")
    List<User> findUserList(@Param("username") String username, 
                           @Param("email") String email, 
                           @Param("phone") String phone,
                           @Param("offset") int offset,
                           @Param("size") int size);

    @Select("SELECT COUNT(*) FROM users " +
           "WHERE (#{username} IS NULL OR username LIKE CONCAT('%', #{username}, '%')) " +
           "AND (#{email} IS NULL OR email LIKE CONCAT('%', #{email}, '%')) " +
           "AND (#{phone} IS NULL OR phone LIKE CONCAT('%', #{phone}, '%'))")
    long countUserList(@Param("username") String username, 
                      @Param("email") String email, 
                      @Param("phone") String phone);
} 